package com.etoak.controller;

import com.etoak.entity.User;
import com.etoak.service.UserService;
import com.etoak.vo.ResultVO;
import jakarta.validation.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Validated
public class UserController {

    @Autowired
    UserService userService;

    @RequestMapping("/test")
    public ResultVO<Object> test(@NotBlank(message = "name不能为空") String name) {
        return ResultVO.success();
    }

    /**
     * post /user
     */
    @PostMapping
    public ResultVO<Object> addUser(@Validated @RequestBody User user) {
        userService.add(user);
        return ResultVO.success();
    }

    /**
     * get /user/{id}
     * http://localhost:8089/user/1    查询id=1的用户详情
     */
    @GetMapping("/{id}")
    public ResultVO<User> getUser(@PathVariable int id) {
        User user = userService.getById(id);
        return ResultVO.success(user);
    }

    /**
     * PUT /user/{id}
     */
    @PutMapping("/{id}")
    public ResultVO<Object> update(@PathVariable int id, @RequestBody User user) {
        user.setId(id);
        userService.updateById(user);
        return ResultVO.success();
    }


    /**
     * 删除 delete /user/{id}
     */
    @DeleteMapping("/{id}")
    public ResultVO<Object> delete(@PathVariable int id) {
        userService.removeById(id);
        return ResultVO.success();
    }
}
